package com.youwu.weather.controller;

import com.youwu.weather.service.WeatherReportService;
import com.youwu.weather.vo.City;
import com.youwu.weather.vo.HeWeather6;
import com.youwu.weather.vo.WeatherType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Weather Controller.
 */
@RestController
@RequestMapping("/report")
public class WeatherReportController {

	@Autowired
	private WeatherReportService weatherReportService;

	@GetMapping("/now/{city}")
	@ResponseBody
	public ModelAndView getReportCurrentWeather(@PathVariable("city") String cityName, Model model) {
		List<HeWeather6> data = weatherReportService.getDataByCity(WeatherType.NOW, cityName);
		// TODO 从城市数据api微服务提供
		List<City> cityList = new ArrayList<>();
		// 写法一
		model.addAttribute("title", "天气预报");
		model.addAttribute("cityName", cityName);
		model.addAttribute("cityList", cityList);
		model.addAttribute("weather", CollectionUtils.isEmpty(data) ? null : data.get(0));
		return new ModelAndView("weather/report/now", "reportNow", model);
	}

	@GetMapping("/forecast/{city}")
	@ResponseBody
	public ModelAndView getForecastWeather(@PathVariable("city") String cityName) {
		List<HeWeather6> data = weatherReportService.getDataByCity(WeatherType.FORECAST, cityName);
		// 写法二
		Map<String, Object> map = new HashMap<>();
		map.put("weather", data);
		return new ModelAndView("weather/report/forecast", "reportForecast", map);
	}

	@GetMapping("/hourly/{city}")
	@ResponseBody
	public ModelAndView getHourlyWeather(@PathVariable("city") String cityName) {
		List<HeWeather6> data = weatherReportService.getDataByCity(WeatherType.HOURLY, cityName);
		Map<String, Object> map = new HashMap<>();
		map.put("weather", data);
		return new ModelAndView("weather/report/hourly", map);
	}

	@GetMapping("/lifestyle/{city}")
	@ResponseBody
	public ModelAndView getLifestyleWeather(@PathVariable("city") String cityName) {
		List<HeWeather6> data = weatherReportService.getDataByCity(WeatherType.LIFESTYLE, cityName);
		Map<String, Object> map = new HashMap<>();
		map.put("weather", data);
		return new ModelAndView("weather/report/lifestyle", map);
	}

}
